package com.beetlestech.nfe.main.biz.com.impl;
import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVHomeWorkListForTeacher;
import com.beetlestech.nfe.main.biz.com.HbVSendHomeWorkListForTeacher;
import com.beetlestech.nfe.main.biz.com.dto.VhomeworklistforteacherInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.VsendworkinfoforteacherlistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.view.VhomeworklistforteacherDTO;
import com.beetlestech.nfe.main.db.view.VsendworkinfoforteacherlistDTO;
public class HbVSendHomeWorkListForTeacherImpl extends AbstractDBLogic implements HbVSendHomeWorkListForTeacher, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	@Override
	public VsendworkinfoforteacherlistInfoDTO[] getHomeWorkHistoryListForTeacher(
			String actionType,
			String inCourseTypeSql,
			String inReceiverIdSql,
			String homeWorkHistoryName,
			Integer courseTypeId,
			String userId
			) {
		if(actionType.equals("many")){
			String dbInCourseTypeSql="catagory_c_id in("+inCourseTypeSql+")";
			String dbInReceiverIdSql="receiver_id in("+inReceiverIdSql+")";
			List<VsendworkinfoforteacherlistInfoDTO> sendHomeWorkList = new ArrayList<VsendworkinfoforteacherlistInfoDTO>();
			List<VsendworkinfoforteacherlistDTO> sendHomeWorkListDB = new ArrayList<VsendworkinfoforteacherlistDTO>();
			VsendworkinfoforteacherlistDTO vsendworkinfoforteacherlistDTO =new VsendworkinfoforteacherlistDTO();
			StatementDTO statementDTO = new StatementDTO();
			if(courseTypeId!=0){
				StatementUtil.andWhere(statementDTO, VsendworkinfoforteacherlistDTO.COLUMN_CATAGORY_C_ID, StatementUtil.KEYWORD.EQUAL ,courseTypeId);
			}
			if(StringUtil.isNotEmpty(homeWorkHistoryName)){
				StatementUtil.andWhere(statementDTO, VsendworkinfoforteacherlistDTO.COLUMN_NAME, StatementUtil.KEYWORD.LIKE ,homeWorkHistoryName);
			}
			if(statementDTO.getSQLStatement()!=null && courseTypeId!=0 ){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement()+" and "+dbInReceiverIdSql);
			}
			if(statementDTO.getSQLStatement()!=null && courseTypeId==0){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement()+" and "+dbInReceiverIdSql+" and "+dbInCourseTypeSql);
			}
			if(statementDTO.getSQLStatement()==null && courseTypeId==0){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(dbInReceiverIdSql+" and "+dbInCourseTypeSql);

			}
			sendHomeWorkListDB=queryDAO.queryForObjectList(VsendworkinfoforteacherlistDTO.SQL_SEARCH, vsendworkinfoforteacherlistDTO);
			if(sendHomeWorkListDB==null || sendHomeWorkListDB.size()==0){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VsendworkinfoforteacherlistDTO item:sendHomeWorkListDB){
					sendHomeWorkList.add(convertToVSendhomeWorkListInfo(item));
				}
			}
	     
		    return  sendHomeWorkList.toArray(new VsendworkinfoforteacherlistInfoDTO[sendHomeWorkList.size()]);
		}
		else{
			String dbInCourseTypeSql="catagory_c_id in ("+inCourseTypeSql+")";
			List<VsendworkinfoforteacherlistInfoDTO> sendHomeWorkList = new ArrayList<VsendworkinfoforteacherlistInfoDTO>();
			List<VsendworkinfoforteacherlistDTO> sendHomeWorkListDB = new ArrayList<VsendworkinfoforteacherlistDTO>();
			VsendworkinfoforteacherlistDTO vsendworkinfoforteacherlistDTO =new VsendworkinfoforteacherlistDTO();
			StatementDTO statementDTO = new StatementDTO();
			if(courseTypeId!=0){
				StatementUtil.andWhere(statementDTO, VsendworkinfoforteacherlistDTO.COLUMN_CATAGORY_C_ID, StatementUtil.KEYWORD.EQUAL ,courseTypeId);
			}
			if(StringUtil.isNotEmpty(homeWorkHistoryName)){
				StatementUtil.andWhere(statementDTO, VsendworkinfoforteacherlistDTO.COLUMN_NAME, StatementUtil.KEYWORD.LIKE ,homeWorkHistoryName);
			}
			/*if(StringUtil.isNotEmpty(userId)){
				StatementUtil.andWhere(statementDTO, VsendworkinfoforteacherlistDTO.COLUMN_RECEIVER_ID, StatementUtil.KEYWORD.EQUAL ,userId);

			}*/
			if(statementDTO.getSQLStatement()!=null && courseTypeId!=0 ){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
			}
			if(statementDTO.getSQLStatement()!=null && courseTypeId==0){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement()+" and "+dbInCourseTypeSql);
			}
			if(statementDTO.getSQLStatement()==null && courseTypeId==0){
				vsendworkinfoforteacherlistDTO.set_dynamicWhereCondition(dbInCourseTypeSql);

			}
			sendHomeWorkListDB=queryDAO.queryForObjectList(VsendworkinfoforteacherlistDTO.SQL_SEARCH, vsendworkinfoforteacherlistDTO);
			if(sendHomeWorkListDB==null || sendHomeWorkListDB.size()==0){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VsendworkinfoforteacherlistDTO item:sendHomeWorkListDB){
					sendHomeWorkList.add(convertToVSendhomeWorkListInfo(item));
				}
			}
		    return  sendHomeWorkList.toArray(new VsendworkinfoforteacherlistInfoDTO[sendHomeWorkList.size()]);
		
		}
	}
	private VsendworkinfoforteacherlistInfoDTO convertToVSendhomeWorkListInfo(VsendworkinfoforteacherlistDTO item){
		VsendworkinfoforteacherlistInfoDTO vsendworkinfoforteacherlistInfoDTO =new VsendworkinfoforteacherlistInfoDTO();
		vsendworkinfoforteacherlistInfoDTO.setcatagorycname(item.getcatagorycname());
		vsendworkinfoforteacherlistInfoDTO.setfileid(item.getfileid());
		vsendworkinfoforteacherlistInfoDTO.sethistoryid(item.gethistoryid());
		vsendworkinfoforteacherlistInfoDTO.setname(item.getname());
		vsendworkinfoforteacherlistInfoDTO.sethomeworkid(item.gethomeworkid());
		return vsendworkinfoforteacherlistInfoDTO;
	}

	
}
